using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Repository.Properties;
using OpenComposite.Base.Collections;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCFolder : RepositoryItemUI
	{
		#region Initialization
		public UCFolder(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			_serviceProvider = serviceProvider;

			InitializeComponent();

			init();
		}
		private void init()
		{
			cboTypes.Items.Clear();
			foreach ( ItemType type in Enum.GetValues(typeof(ItemType)) ) {
				cboTypes.Items.Add(new ListItem(ItemTypes.GetDisplay(type), type));
			}

			//
			// _folderList
			//
			_folderList = new UCRepositoryItemList(_serviceProvider);
			_folderList.Dock = DockStyle.Fill;
			groupItems.Controls.Add(_folderList);
		}
		#endregion

		#region Protected Members

		#region Properties
		public override bool CanDelete
		{
			get
			{
				if ( _folder == null ) return false;
				BindingListEx<IRepositoryItem> items = Global.Data.GetRepositoryHierarchyParentsList(_folder);
				if ( items.Count == 0 ) return true;
				IRepositoryItem item = items[items.Count - 1];
				bool deletable = _folder.IsUserFolder ||
					( !( item is OpenComposite.EII.Repository.Domain ) &&
					  !( item is OpenComposite.EII.Repository.Workflow ) );
				return deletable;
			}
		}
		#endregion

		#endregion

		#region Public Members

		#region Properties
		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _folder;
			}
			set
			{
				if ( value is GenericFolder ) {
					_folder = (GenericFolder)value;
					load();
				}
			}
		}

		public override string TypeText
		{
			get { return Resources.sFolder; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private GenericFolder _folder = null;
		private UCRepositoryItemList _folderList = null;
		IServiceProvider _serviceProvider;
		#endregion

		#region Methods
		private void load()
		{
			if ( _folder == null )
				return;

			_folder.Reload();

			txtName.Text = _folder.Name;
			txtDesc.Text = _folder.Description;

			// load ItemTypes
			cboTypes.BeginUpdate();
			try {
				clearItemTypesCombo();
				List<ItemType> types = new List<ItemType>(_folder.ItemTypes);
				for ( int i = 0; i < cboTypes.Items.Count; i++ ) {
					if ( types.Contains(( (ListItem)cboTypes.Items[i] ).itemtype) )
						cboTypes.SetItemChecked(i, true);
				}
				if ( _folder.ItemTypes.Length == 1 && _folder.ItemTypes[0] == ItemType.Noun ) {
					_folderList.CustomValueProperty = "custom";
					_folderList.CustomValueColumnHeader = "Data Type";
					_folderList.CustomValueVisible = true;
				} else if ( _folder.ItemTypes.Length == 1 && _folder.ItemTypes[0] == ItemType.RuleConstant ) {
					_folderList.CustomValueProperty = "custom";
					_folderList.CustomValueColumnHeader = "Value";
					_folderList.CustomValueVisible = true;
				} else {
					_folderList.CustomValueProperty = "";
					_folderList.CustomValueColumnHeader = "";
					_folderList.CustomValueVisible = false;
				}
				cboTypes.Enabled = _folder.IsUserFolder;
			} finally {
				cboTypes.EndUpdate();
			}
			_folderList.FolderID = _folder.ID;
		}
		private void clearItemTypesCombo()
		{
			cboTypes.BeginUpdate();
			try {
				cboTypes.Text = "";
				cboTypes.SelectedIndex = -1;
				for ( int i = 0; i < cboTypes.Items.Count; i++ ) {
					cboTypes.SetItemChecked(i, false);
				}
			} finally {
				cboTypes.EndUpdate();
			}
		}
		private void save()
		{
			if ( _folder == null )
				_folder = new GenericFolder();

			_folder.Reload();

			_folder.Name = txtName.Text;
			_folder.Description = txtDesc.Text;

			// save ItemTypes
			if ( _folder.IsUserFolder ) {
				List<ItemType> types = new List<ItemType>();
				foreach ( ListItem item in cboTypes.CheckedItems ) {
					types.Add(item.itemtype);
				}
				_folder.ItemTypes = types.ToArray();
			}
		}
		#endregion

		#region Event Handlers
		#endregion

		#endregion

		private class ListItem
		{
			public ListItem(string disp, ItemType type)
			{
				display = disp;
				itemtype = type;
			}
			public string display;
			public ItemType itemtype;
			public override string ToString()
			{
				return display;
			}
		}
	}
}
